package com.microcity.autocomponent.autoview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.microcity.autocomponent.R;
import com.microcity.autocomponent.uitls.UiUtils;

/**
 * Created by Darksiderl on 2017/3/30.
 */
public class LBikeProgressBar extends View {

    //背景画笔
    private Paint mBgPaint;
    //背景画笔颜色
    private int mBgPaintColor = Color.parseColor("#aaffffff");
    //进度图片
    private Bitmap mProgressIcon;
    //进度图片的高度 背景需要根据图片高度设置
    private int mProgressIconHeight;
    //默认的padding
    private int mBorderPadding = 6;
    //控件宽度
    private int mWidthMeasureSpecsize;
    //控件高度
    private int mHeight;
    //背景rectF
    private RectF rectF;
    //进度
    private int mProgress = 0;
    //进度图片资源ID
    private int mProgressResId;


    public LBikeProgressBar(Context context) {
        this(context, null, 0);
    }

    public LBikeProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LBikeProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //初始化参数设置
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LBikeProgressBar);
        mProgress = typedArray.getInteger(R.styleable.LBikeProgressBar_lbike_progress, 0);
        mBgPaintColor = typedArray.getColor(R.styleable.LBikeProgressBar_lbike_progressbarcolor, Color.parseColor("#aaffffff"));
        mProgressResId = typedArray.getResourceId(R.styleable.LBikeProgressBar_lbike_progressbaricon, R.mipmap.ic_ubike_unlock_progress_icon);
        typedArray.recycle();
        Log.d("mProgress", mProgress + "");
        Log.d("mBgPaintColor", mBgPaintColor + "   " + Color.parseColor("#aaffffff"));
        Log.d("mProgressResId", mProgressResId + "   " + R.mipmap.ic_ubike_unlock_progress_icon);

        //初始化背景画笔
        initPaint();
        //初始化bitmap并且获取大小
        initBitmapAndGetSize();
    }

    private void initPaint() {
        mBgPaint = new Paint();
        mBgPaint.setAntiAlias(true);
        Log.d("mBgPaintColor", mBgPaintColor + "");
        mBgPaint.setColor(mBgPaintColor);//mBgPaintColor
//        mBgPaint.setDither(true);//防抖动
        mBgPaint.setStyle(Paint.Style.FILL);
    }

    private void initBitmapAndGetSize() {
        //设置默认的padding
        mBorderPadding = dp2px(6);
        BitmapFactory.Options options = new BitmapFactory.Options();
        //获取进度图片
        mProgressIcon = BitmapFactory.
                decodeResource(getContext().getResources(), mProgressResId, options);
        //bar的高度（底部进度条的高度）
        mHeight = options.outHeight + mBorderPadding;
        Log.d("mHeight", mHeight + "");
        //进度图片的高度 和bar高度一致不好看
        mProgressIconHeight = mProgressIcon.getHeight();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidthMeasureSpecsize = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(mWidthMeasureSpecsize, mHeight);
        rectF = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight());
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRoundRect(rectF, mHeight, mHeight, mBgPaint);
        float left = mBorderPadding / 2f + mProgress / 100f * (getMeasuredWidth() - mProgressIcon.getWidth() - mBorderPadding);
        canvas.drawBitmap(mProgressIcon, left, mBorderPadding / 2f, null);
    }

    //设置进度
    public void setmProgress(int mProgress) {
        this.mProgress = mProgress;
        invalidate();
    }

    private int dp2px(int dp) {
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * density);
    }
}
